package com.showboom.elauncher.services;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.os.SystemClock;
import android.provider.Settings;
import android.util.Log;

import com.showboom.elauncher.MyAppliacation;
import com.showboom.elauncher.util.HttpUtils;
import com.showboom.elauncher.util.Util;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by cface on 2018/3/8.
 */

public class HeartService extends Service {
    private final String TAG = "Launcher_HeartService";
    private long last_time = 0;
    private static final String PARAM_SERVICE = "heartbeat";
    private static final String CP_ZHANGDUAN = "8";

    TimerTask HeartTask = new TimerTask() {
        @Override
        public void run() {
            long current_time = System.currentTimeMillis();//获取当前时间
            long interval_time = current_time - last_time;
            if (interval_time >= 30 * 60 * 1000) { //半小时上报一次
                int workStatus = Settings.System.getInt(MyAppliacation.context.getContentResolver(), "showboom_work_status", 0);
                if(workStatus == 1) {
                    createSendHeartThread(getApplicationContext());
                }
                last_time = System.currentTimeMillis(); //记录最后一次上报时间
            }
        }
    };

    private void createSendHeartThread(final Context context) {

        String imei = Util.getImei(context);
        if (null == imei) imei = "";

        final Map<String, String> param = new HashMap<>();
        param.put("service", PARAM_SERVICE);
        param.put("imei", imei);
        param.put("cp", CP_ZHANGDUAN);

        new Thread(new Runnable() {
            @Override
            public void run() {
                int count = 3;
                for (int i = 0; i < count; i++) {
                    String result = HttpUtils.submitPostData(Util.heart_Url, param, "utf-8");
                    Log.d(TAG, "HeartbeatPacket:" + "result: " + result);

                    if (!result.equals("-1") && !result.startsWith("err")) {
                        try {
                            JSONObject jsonObject = new JSONObject(result);
                            int code = jsonObject.getInt("code");
                            if (code == 0) {
                                break;
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    try {
                        Thread.sleep(5 * 1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        last_time = System.currentTimeMillis();
        Timer timer = new Timer();
        timer.schedule(HeartTask, 0, 30 * 1000);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
